//
//  PushWeathController.m
//  UI_Project_A
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "PushWeathController.h"
#import "ModelOfData.h"
#import <UIImageView+WebCache.h>
#import <DKNightVersion.h>

@interface PushWeathController ()

@property (nonatomic ,retain)UIScrollView *scroll;
@property (nonatomic ,retain)UIView *viewOfBottom;
@property (nonatomic ,retain)NSString *strImage;
@property (nonatomic ,retain)NSMutableArray *arrData;
@property (nonatomic ,retain)UILabel *titleLabel;
@property (nonatomic, assign) CGFloat heightOfAll;
@property (nonatomic ,assign)BOOL isSelected;
@property (nonatomic ,assign)BOOL isBadSelected;
@property (nonatomic ,retain)UIButton *buttonOfBad;
@property (nonatomic ,retain)UIButton *buttonOfGood;
@property (nonatomic ,assign)CGFloat h;


@end

@implementation PushWeathController


- (void)dealloc {
    [_buttonOfBad release];
    [_buttonOfGood release];
    [_viewOfBottom release];
    [_titleLabel release];
    [_strImage release];
    [_arrData release];
    [_scroll release];
    [super dealloc];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self data];
    [self createView];
    [self createScroll];
    [self createImage];
    [self crteateTitleLabel];
    [self nightDay];
    
    
}


- (void)nightDay {
    
    self.scroll.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
    self.titleLabel.dk_backgroundColorPicker =  DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
    
    self.titleLabel.dk_textColorPicker = DKColorWithColors([UIColor colorWithRed:0.067 green:0.067 blue:0.071 alpha:1.000],[UIColor colorWithWhite:0.811 alpha:1.000]);
    
    
}




- (void)data {
    
    self.strImage = @"";
    self.arrData = [NSMutableArray array];
    for (NSDictionary *dic in self.model.content) {
        
        [dic objectForKey:@"type"];
        
        
        if ([[dic objectForKey:@"type"] isEqualToString:@"image"]) {
            
            NSDictionary *dicData = [dic objectForKey:@"data"];
            NSDictionary *dicurl = [dicData objectForKey:@"big"];
            
            self.strImage = [dicurl objectForKey:@"url"];
            
        }else{
            
            NSString *dicData = [dic objectForKey:@"data"];
            [self.arrData addObject:dicData];
        }
    }
    
}









#pragma mark - 标题
- (void)crteateTitleLabel {
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.numberOfLines = 0;
    [self.scroll addSubview: self.titleLabel];
    self.titleLabel.frame = CGRectMake(20, 20, self.view.frame.size.width - 40, 120);
    [self.titleLabel release];
    self.titleLabel.text = self.model.title;
    self.titleLabel.font = [UIFont systemFontOfSize:30];

    
    UIView *viewOfcolor = [[UIView alloc] initWithFrame:CGRectMake(20,133 , self.view.frame.size.width - 40, 1)];
    [self.scroll addSubview:viewOfcolor];
    

    
}

#pragma mark 加载图片
- (void)createImage {
    
    UIImageView *imageviewOfcontect = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    imageviewOfcontect.frame =  CGRectMake(20, 150, WIDTH-40, 200);
    
    
    
    [self.scroll addSubview:imageviewOfcontect];
    [imageviewOfcontect release];
    
    if (self.model.url){
        
        NSString *url = [self Substring:self.strImage];
    
        [imageviewOfcontect sd_setImageWithURL:[NSURL URLWithString:url]];
    }
}



#pragma mark - 图片Url截取
- (NSString *)Substring:(NSString *)urlString {
    
    
    NSString *sub = urlString;
    
    if ([sub isEqualToString:@""]) {
        return nil;
    }
    if ([sub hasSuffix:@"jpg"]) {
        return urlString;
    }
    else {
        
        NSRange range = [urlString rangeOfString:@"u="];
        
        NSString *sub1 = [urlString substringFromIndex:range.location+2];
        
        NSRange lastRange = [sub1 rangeOfString:@"&"];
        sub = [sub1 substringToIndex:lastRange.location];
        
    }
    
    return sub;
}








#pragma mark - scrollView
- (void)createScroll {
    
    self.scroll = [[UIScrollView alloc] init];
    self.scroll.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 50);
    [self.view addSubview:self.scroll];
 
    
    for (int i = 0; i < self.arrData.count; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.text = self.arrData[i];
        [self.scroll addSubview:label];
        
        
        //night
        label.dk_textColorPicker = DKColorWithColors([UIColor colorWithRed:0.067 green:0.067 blue:0.071 alpha:1.000],[UIColor colorWithWhite:0.811 alpha:1.000]);
        

        
        label.numberOfLines = 0;
        
        if ([self.strImage isEqualToString:@""]) {
            
            label.frame = CGRectMake(20,150 + self.heightOfAll, WIDTH - 40, [self heightForLabel:self.arrData[i]]);
            self.heightOfAll +=[self heightForLabel:self.arrData[i]];
            
        }else{
            
            label.frame = CGRectMake(20,350 + self.heightOfAll, WIDTH - 40, [self heightForLabel:self.arrData[i]]);
            
            self.heightOfAll +=[self heightForLabel:self.arrData[i]];
        }
        [label release];
    }
    
    if (!self.model.url) {
        self.h = [self heightForLabel:self.arrData[0]] + self.heightOfAll+50+150;
        
    }else{
        
        
        self.h = [self heightForLabel:self.arrData[0]] + self.heightOfAll+50+350;
    }
    
    self.scroll.contentSize = CGSizeMake(WIDTH,self.h+200);
    
    [self createGoodButton];
    [self share];

}

- (void)share {
    //
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.h + 20, WIDTH - 20, 50)];
    [self.scroll addSubview:label];
    label.font = [UIFont systemFontOfSize:23];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"- 分享到  -";
    
    label.dk_textColorPicker = DKColorWithColors([UIColor colorWithWhite:0.247 alpha:1.000],[UIColor colorWithWhite:0.596 alpha:1.000]);
    
    UIImageView *imageFX = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"weixin"]];
    [self.scroll addSubview:imageFX];
    imageFX.frame = CGRectMake(WIDTH /5, self.h + 70, 70, 80);
    [imageFX release];
    
    
    UIImageView *imagePYQ = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pengyouquan"]];
    [self.scroll addSubview:imagePYQ];
    imagePYQ.frame = CGRectMake(WIDTH / 5 * 3 , self.h + 70, 70, 80);
    [imagePYQ release];
    
    
//    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ISNIGHT"]) {
//        [imageFX setImage:[UIImage imageNamed:@"night"]];
//        
//    }else {
//        
//        [imageFX setImage:[UIImage imageNamed:@"fenxiang"]];
//        
//    }
    
}



-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(WIDTH - 40 , 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}



#pragma 点赞

- (void)createGoodButton{
    
    
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20 + WIDTH / 3 - 40, self.h - 54, 50, 50)];
    
    [self.scroll addSubview:label];
    label.text = @"好文";
    label.textColor = [UIColor colorWithRed:0.898 green:0.2118 blue:0.1804 alpha:1.0];
    label.textAlignment  = 1;
    label.font = [UIFont systemFontOfSize:23];
    
    
    self.buttonOfGood = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfGood.frame = CGRectMake(20, self.h - 55, WIDTH  / 3 -20 , 50);
    [self.scroll addSubview:self.buttonOfGood];
    
    [self.buttonOfGood setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
    
    
    [self.buttonOfGood addTarget:self action:@selector(clickOfGood:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UILabel *labelBad = [[UILabel alloc] initWithFrame:CGRectMake(20 + WIDTH / 3*2, self.h - 54, 50, 50)];
    
    [self.scroll addSubview:labelBad];
    labelBad.text = @"太水";
    
    //night
    labelBad.dk_textColorPicker = DKColorWithColors([UIColor colorWithRed:0.051 green:0.051 blue:0.055 alpha:1.000], [UIColor colorWithWhite:0.686 alpha:1.000]);
    
    
    labelBad.font = [UIFont systemFontOfSize:23];
    labelBad.textAlignment = 1;
    self.buttonOfBad = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfBad.frame = CGRectMake(WIDTH/2, self.h - 55, WIDTH / 3 - 20, 50);
    [self.scroll addSubview:self.buttonOfBad];
    [self.buttonOfBad setImage:[UIImage imageNamed:@"bad5"] forState:UIControlStateNormal];
    
    [self.buttonOfBad addTarget:self action:@selector(clickoutBadbutton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (void)clickoutBadbutton:(UIButton *)button {
    
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
    keyAnimation.duration = 0.2;
    [button.layer addAnimation:keyAnimation forKey:@"key"];
    
    if (self.isBadSelected == 0) {
        [self.buttonOfBad setImage:[UIImage imageNamed:@"bad4"] forState:UIControlStateNormal];
        
    }else {
        [self.buttonOfBad setImage:[UIImage imageNamed:@"bad5"] forState:UIControlStateNormal];
        
    }
    self.isBadSelected = !self.isBadSelected;
    
    
    
    //
    [self.buttonOfGood setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
    self.isSelected = 0;
    
    
}



- (void)clickOfGood:(UIButton *)button {
    
    
    CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    
    keyAnimtion.values = @[@(0.1),@(1.0),@(1.5)];
    
    keyAnimtion.duration = 0.2;
    
    [button.layer addAnimation:keyAnimtion forKey:@"key"];
    
    
    if (self.isSelected == 0) {
        
        [self.buttonOfGood setImage:[UIImage imageNamed:@"good1"] forState:UIControlStateNormal];
    }else{
        [self.buttonOfGood setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
        
    }
    
    self.isSelected = !self.isSelected;
    
    
    
    [self.buttonOfBad setImage:[UIImage imageNamed:@"bad5"] forState:UIControlStateNormal];
    self.isBadSelected = 0;
    
}









#pragma mark - 下方功能键
- (void)createView {
    
    self.viewOfBottom = [[UIView alloc] init];
    self.viewOfBottom.frame = CGRectMake(0, HEIGHT - 50, WIDTH, 50);
    [self.view addSubview:self.viewOfBottom];
    [self.viewOfBottom release];
    self.viewOfBottom.backgroundColor = [UIColor lightGrayColor];
    [self createButton];
    
}

#pragma mark - 返回功能
- (void)createButton {
    
    
    UIButton *buttonOfDisMiss = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonOfDisMiss.frame = CGRectMake(10, HEIGHT- 50, 40, 40);
    
    
    [buttonOfDisMiss setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [self.view addSubview:buttonOfDisMiss];
    
    [buttonOfDisMiss addTarget:self action:@selector(buttonOfDisMissOfPush:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)buttonOfDisMissOfPush:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
